<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_ChangelogModel extends ADS_DefaultModel {
    
    private function prepare($change){
        
        global $current_user;
        get_currentuserinfo();

        $change['authorId'] = $current_user->ID;
        $change['date']     = date('Y-m-d h:i:s');
        
        return $change;
        
    }

    public function countChanges() {
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_changelog]
          %if', (bool)$this->useFilter, 'WHERE %and', $this->filter
        )->cnt;
    }
    
    public function getChanges() {
        return dibi::fetchAll('
          SELECT            
            *
          FROM
            [:wp:ads_changelog] AS t1
          INNER JOIN 
            [:wp:users] AS t2 ON t1.authorId = t2.ID
           %if', (bool)$this->useFilter, 'WHERE %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
        );
    }
     
    public function getChange($changeId) {
        if(!is_int($changeId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT            
            *
          FROM
            [:wp:ads_changelog] AS t1
          WHERE
            changeId = %i
        ', $changeId);
    }
    
    public function addChange($change){
        if(!is_array($change)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        
        dibi::query('INSERT INTO [:wp:ads_changelog]', $this->prepare($change));
    }
    
    public function editChange($changeId, $change){
        if(!is_array($change) || !is_int($changeId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_changelog] SET', $this->prepare($change), 'WHERE changeId = %i', $changeId);
    }
    
    public function commitChanges($deploymentId){
        if(!is_int($deploymentId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_changelog] SET status = %i, deploymentId = %i WHERE status = %i', 1, $deploymentId, 0);
    }
    
    public function removeChange($changeId){
        if(!is_int($changeId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        $change = $this->getChange($changeId);
        //update positions
        dibi::query('DELETE FROM [:wp:ads_changelog] WHERE changeId = %i LIMIT 1', $changeId);
    }

}